import constant from "../../utils/constant";
import util from "../../utils/util";
import api from "../../utils/api";

Page({
    data: {
        MINIO: constant.MINIO_COURSE_COVER,
        PROJECT_TITLE: constant.PROJECT_TITLE,
        PROJECT_SUB_TITLE: constant.PROJECT_SUB_TITLE,
        MINIO_BANNER: constant.MINIO_BANNER,
        banners: null,
        articles: null,
        seckills: null,
        currentNotice: null,
        activeNewsName: 1,
        activeSeckillIdx: 0
    },

    // 查询1条通知记录
    topNotice1: function () {
        let that = this;
        api.get('notice', '/top/1')
            .then(res => that.setData({currentNotice: res[0]['content']}))
            .catch(err => util.error('查询失败', err));
    },
    // 查询5条轮播图记录
    topBanner5: function () {
        let that = this;
        api.get('banner', '/top/5')
            .then(res => that.setData({banners: res}))
            .catch(err => util.error('查询失败', err));
    },
    // 查询5条通知记录
    topArticle5: function () {
        let that = this;
        api.get('article', '/top/5')
            .then(res => that.setData({articles: res}))
            .catch(err => util.error('查询失败', err));
    },

    // 查询10条未开始的营销活动
    nearSeckill10: function () {
        let that = this;
        api.get('seckill', '/near/10')
            .then(res => that.setData({seckills: res}))
            .catch(err => util.error('查询失败', err));
    },
    // 切换秒杀活动
    changeSeckill: function (ev) {
        this.setData({'activeSeckillIdx': ev.detail});
    },
    // 切换公告
    changeArticle: function (ev) {
        this.setData({'activeArticleName': ev.detail});
    },
    onLoad: function (options) {
        this.topNotice1();
        this.topBanner5();
        this.topArticle5();
        this.nearSeckill10();
        this.getTabBar().setData({"activeTab": 0});
    }
});